

package com.hazelcast.core;

/**
 * Type of entry event.
 */
public enum EntryEventType {

    /**
     * Fired if an entry is added.
     */
    ADDED(TypeId.ADDED),

    /**
     * Fired if an entry is removed.
     */
    REMOVED(TypeId.REMOVED),

    /**
     * Fired if an entry is updated.
     */
    UPDATED(TypeId.UPDATED),

    /**
     * Fired if an entry is evicted.
     */
    EVICTED(TypeId.EVICTED),

    /**
     * Fired if an entry is expired.
     */
    EXPIRED(TypeId.EXPIRED),
    /**
     * Fired if all entries are evicted.
     */
    EVICT_ALL(TypeId.EVICT_ALL),

    /**
     * Fired if all entries are cleared.
     */
    CLEAR_ALL(TypeId.CLEAR_ALL),

    /**
     * Fired if an entry is merged after a network partition.
     */
    MERGED(TypeId.MERGED),

    /**
     * Fired if an entry is invalidated.
     */
    INVALIDATION(TypeId.INVALIDATION),

    /**
     * Fired if an entry is loaded.
     */
    LOADED(TypeId.LOADED);

    private final int typeId;

    EntryEventType(final int typeId) {
        this.typeId = typeId;
    }

    /**
     * @return the matching EntryEventType for the supplied {@code typeId}
     * or {@code null} if there is no match
     */
    public static EntryEventType getByType(final int typeId) {
        return switch (typeId) {
            case TypeId.ADDED -> ADDED;
            case TypeId.REMOVED -> REMOVED;
            case TypeId.UPDATED -> UPDATED;
            case TypeId.EVICTED -> EVICTED;
            case TypeId.EVICT_ALL -> EVICT_ALL;
            case TypeId.CLEAR_ALL -> CLEAR_ALL;
            case TypeId.MERGED -> MERGED;
            case TypeId.EXPIRED -> EXPIRED;
            case TypeId.INVALIDATION -> INVALIDATION;
            case TypeId.LOADED -> LOADED;
            default -> null;
        };
    }

    /**
     * @return the event type ID
     */
    public int getType() {
        return typeId;
    }

    /**
     * These constants represent event type ID and bit-mask of events.
     *
     * @see com.hazelcast.map.impl.MapListenerFlagOperator
     */
    @SuppressWarnings("checkstyle:magicnumber")
    private static class TypeId {
        private static final int ADDED = 1;
        private static final int REMOVED = 1 << 1;
        private static final int UPDATED = 1 << 2;
        private static final int EVICTED = 1 << 3;
        private static final int EXPIRED = 1 << 4;
        private static final int EVICT_ALL = 1 << 5;
        private static final int CLEAR_ALL = 1 << 6;
        private static final int MERGED = 1 << 7;
        private static final int INVALIDATION = 1 << 8;
        private static final int LOADED = 1 << 9;
    }
}
